<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分页完整版</title>
    <script src="js/jquery-3.6.0.js"></script>
</head>

<body>
    <table border="1" class="table_family">
        <tr>
            <th>头像</th>
            <th>名字</th>
            <th>姓</th>
            <th>生日</th>
            <th>电话</th>
            <th>性别</th>
            <th>操作</th>
        </tr>
        <tbody id="tbbb"> </tbody>
    </table>
    <div style="margin-top: 20px;">
        <button onclick="prevPage()">上一页</button>
        <span id="btn_wrap"> </span>
        <button onclick="nextPage()">下一页</button>
    </div>

    <script>
        let page = 1;
        let pageSize = 2;
        let total = 0; //总数
        //分页功能
        /**
         * @param1 currentPage 表示你要展示第几页
         * @param2 pageSize 表示你每页要展示几条数据
         **/
        function getListByPage(currentPage, pageSize) {
            // console.log(currentPage);
            $.ajax({
                type: "get",
                url: "http://39.106.207.209:8100/family/getFamiliesList/" + currentPage + "/" + pageSize,
                success: function(res) {
                    // console.log(res);
                    let str = "";
                    total = res.total; //切换总数
                    //渲染 列表
                    for (let index = 0; index < res.data.length; index++) {
                        str += `<tr>
                                <td>
                                    <img src="http://39.106.207.209:8100/clansman${res.data[index].headimg}" />
                                </td>
                                <td>${res.data[index].surname}</td>
                                <td>${res.data[index].name}</td>
                                <td>${res.data[index].gender}</td>
                                <td>${res.data[index].birth_greg}</td>
                                <td>${res.data[index].phone}</td>
                                <td>${res.data[index].life_motto}</td>
                            </tr>`;
                    }
                    $("#tbbb").html(str)
                        //渲染按钮
                        //1.需要多少个按钮
                    let len = Math.ceil(total / pageSize);
                    let btn = ''
                    for (let i = 0; i < len; i++) {
                        btn += `<button onclick='pagenum(${i + 1})'>${i + 1}</button>`
                    }
                    $('#btn_wrap').html(btn)
                }
            })
        }
        getListByPage(page, pageSize)
            // 下一页
        function nextPage() {
            //最多可以分多少页 Math.ceil(total / 2)
            if (page < Math.ceil(total / pageSize)) {
                page++;
                getListByPage(page, pageSize)
            } else {
                alert("最后一页啦")
            }
        }
        // 上一页
        function prevPage() {
            if (page > 1) {
                page--;
                getListByPage(page, pageSize)
            } else {
                alert("第一页啦")
            }
        }
        // 点击页数按钮
        function pagenum(a) {
            getListByPage(a, pageSize)
        }
    </script>

</body>

</html>